<?php
/**
 * 付款纪录增加车主id
 * @author angl
 */

date_default_timezone_set("Asia/Shanghai");
set_time_limit(0);
if (isset($_SERVER['REQUEST_URI'])) {
    die('error');
}

if (!defined('API_ROOT_PATH')) {
    define('API_ROOT_PATH', dirname(__FILE__));
}

require(API_ROOT_PATH . '/../inc/common.php');
require(API_ROOT_PATH . '/../inc/Config.php');
require(API_ROOT_PATH . '/../inc/MysqliDb.php');

//初始化数据库链接
function init_db()
{
    $db = new MysqliDb ([
        'host' => MYSQLDB,
        'username' => MYSQLDB_USER,
        'password' => MYSQLDB_PWD,
        'db' => MYSQLDB_DATABASE,
        'port' => 3306,
        'prefix' => 'au_',
        'charset' => 'utf8'
    ]);
    return $db;
}

$db = init_db();
$list = $db->where('status',0)->get('pay_log');
logUtil("共".count($list)."条纪录",'INFO');
$counter = 0;
foreach($list as $kl=>$vl){
   $result = $db->rawQueryOne("SELECT f.fid,f.bank_name,f.card_no,f.account_name FROM au_pay_log as pl 
          INNER JOIN au_cars as c on c.car_id=pl.car_id
          INNER JOIN au_car_owner as co on co.owner_id=c.owner_id
          LEFT JOIN au_fours as f on f.channel_id=co.comefrom
          WHERE c.car_source=1 AND pl.car_id={$vl['car_id']}");
    if(empty($result)) continue;
    $bank_account = [
        'bank_name' => $result['bank_name'],
        'card_no' => $result['card_no'],
        'account_name' => $result['account_name'],
    ];
    $ret = $db->where('pay_id',$vl['pay_id'])->update('pay_log',[
        'bank_account' => json_encode($bank_account,JSON_UNESCAPED_UNICODE),
        'fid' => $result['fid']
    ]);
    if($ret){
        logUtil("order: ".$vl['order_id']." 处理完成",'INFO');
        $counter++;
    }
}
logUtil($counter." 条数据处理完成","INFO");
